﻿namespace SAD.DataCenter
{
    using System;
    using System.Linq;
    using System.Net.Sockets;
    using System.Text;
    using SAD.Common.Models;
    using System.ServiceModel;
    using SAD.Common;
    using System.ServiceModel.Description;
    using System.Collections.Generic;
    using SAD.Common.Contracts.ShowNode;
    using System.IO;

    public class MainActivator
    {
        #region Instance
        /// <summary>
        /// Activator实例
        /// </summary>
        public static MainActivator Current
        {
            get
            {
                return m_Current;
            }
        }
        private static MainActivator m_Current = new MainActivator();
        #endregion

        private System.Threading.Thread m_testInfoSendThread = null;

        private List<ServiceHost> m_Hosts = null;

        public bool Prepared { get; private set; }

        public List<Common.Models.FilterWord> FilterWords { get; private set; }

        private MainActivator()
        {
            this.FilterWords = new List<FilterWord>();

            m_Hosts = new List<ServiceHost>();
            this.m_Hosts.Add(new ServiceHost(typeof(ShowNodeManagerService)));
            this.m_Hosts.Add(new ServiceHost(typeof(InfoCategoryManagerService)));
            this.m_Hosts.Add(new ServiceHost(typeof(InfoManagerService)));
        }

        private int fastToLower(char character)
        {
            //将大写英文字母以及全/半角的英文字母转化为小写字母
            int charVal = (int)character;
            if (charVal <= 90)
            {
                if (charVal >= 65) //字母A-Z
                    return charVal - 65 + 97;
            }
            else if (charVal >= 65313)
            {
                if (charVal <= 65338)
                    return charVal - 65313 + 97; //全角大写A-Z
                else if (charVal >= 65345 && charVal <= 65370)
                    return charVal - 65345 + 97; //全角小写a-z
            }
            return charVal;
        }

        public void Start()
        {
            if (!Prepared)
            {
                //
                using (SAD_Modles ctx = new SAD_Modles())
                {
                    var q = from filterWord in ctx.FilterWords orderby filterWord.Level descending select filterWord;
                    FilterWords.AddRange(q.ToArray());
                }

                //
                foreach (var host in m_Hosts)
                {
                    host.Open();
                }

                //
                InfoSourceManager.Instance.Start();

                //
                m_testInfoSendThread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(delegate(object obj)
                {
                    while (true)
                    {
                        ShowNode_Data showNode = null;
                        for (int i = 0; i < ShowNodeManager.Instance.OnlineShowNodes.Count; i++)
                        {
                            showNode = ShowNodeManager.Instance.OnlineShowNodes[i];
                            try
                            {
                                if (showNode.OnlineStatus == 1)
                                {
                                    long infoID1 = InfoManager.Instance.SendInfo(1, 0, SAD.Common.Enums.Info_ShowType.NormalText, "现有2室一厅房屋出租，有意请拨打电话：13852523636", 0, SAD.Common.Enums.Info_SourceType.SMS, "system");
                                    long infoID2 = InfoManager.Instance.SendInfo(1, 0, SAD.Common.Enums.Info_ShowType.NormalText, "自由门", 0, SAD.Common.Enums.Info_SourceType.SMS, "system");
                                }
                            }
                            catch (System.ServiceModel.EndpointNotFoundException ex)
                            {
                                ShowNodeManager.Instance.SetOffline(showNode.Mark);
                                Exception exx = ex;
                            }
                            catch (Exception ex)
                            {
                                Exception exx = ex;
                            }
                        }

                        System.Threading.Thread.Sleep(1000);
                    }
                }));
                //m_testInfoSendThread.Start();

                Prepared = true;
            }
        }

        public void Stop()
        {
            if (!Prepared)
            {
                m_testInfoSendThread.Abort();
                m_testInfoSendThread = null;

                //
                InfoSourceManager.Instance.Stop();

                foreach (var host in m_Hosts)
                {
                    host.Close();
                }

                Prepared = false;
            }
        }
    }
}
